package com.chen.authority.controller;

import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.common.util.ExcelUtil;
import com.chen.authority.common.util.WrapperUtil;
import com.chen.authority.model.dto.DictTypeDto;
import com.chen.authority.model.qo.DictTypeQo;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.vo.DictTypeVo;
import com.chen.authority.service.DictTypeService;
import lombok.RequiredArgsConstructor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.util.Arrays;
import java.util.List;

/**
 * 字典类型管理
 *
 * @author chengy
 * @date 2023-09-04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/dict-type")
@Api(tags = "字典类型管理 DictTypeController")
public class DictTypeController {

    private final DictTypeService dictTypeService;

    @GetMapping("/list")
    @ApiOperation("查询字典类型列表")
    @SaCheckPermission("system:dict:list")
    public PageResponse<DictTypeVo> page(PageQo pageQo, DictTypeQo dictTypeQo) {
        return PageResponse.success(dictTypeService.pageQueryDictTypeList(pageQo, dictTypeQo));
    }

    @GetMapping("/{id}")
    @ApiOperation("查询字典类型详细信息")
    @SaCheckPermission("system:dict:update")
    public SingleResponse<DictTypeVo> get(@PathVariable Integer id) {
        return SingleResponse.success(dictTypeService.getById(id).toVo(DictTypeVo.class));
    }

    @PostMapping
    @ApiOperation("新增字典类型")
    @SaCheckPermission("system:dict:add")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.INSERT)
    public Response add(@Valid @RequestBody DictTypeDto dto) {
        return dictTypeService.addOrUpdateDictType(null, dto);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新字典类型")
    @SaCheckPermission("system:dict:update")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.UPDATE)
    public Response update(@PathVariable Integer id, @Valid @RequestBody DictTypeDto dto) {
        return dictTypeService.addOrUpdateDictType(id, dto);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除字典类型")
    @SaCheckPermission("system:dict:delete")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.DELETE)
    public Response delete(@PathVariable Integer[] ids) {
        return dictTypeService.deleteDictTypeByIds(Arrays.asList(ids));
    }

    @PostMapping("/export")
    @ApiOperation("导出字典类型")
    @SaCheckPermission("system:dict:export")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, DictTypeQo qo) {
        List<DictTypeVo> dictTypes = dictTypeService.exportDictTypeList(WrapperUtil.buildQueryWrapper(qo));
        ExcelUtil.exportExcel(response, "字典类型表", DictTypeVo.class, dictTypes);
    }

}